home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / genctxt.zip / CHAP2.TXT < prev    next >
Text File  |  1987-11-21  |  15KB  |  323 lines

  1.  
  2.                       Chapter 2 - Getting started in C
  3.  
  4.  
  5.                             YOUR FIRST C PROGRAM
  6.  
  7.              The best way to get started with C is to actually  look
  8.         at  a program, so load the file named TRIVIAL.C and  display
  9.         it on the monitor.  You are looking at the simplest possible
  10.         C  program.  There is no way to simplify this program or  to
  11.         leave  anything out.  Unfortunately, the program doesn't  do
  12.         anything.
  13.  
  14.              The  word  "main" is very important,  and  must  appear
  15.         once,  and only once in every C program.   This is the point
  16.         where execution is begun when the program is run.   We  will
  17.         see  later that this does not have to be the first statement
  18.         in  the  program  but  it must exist  as  the  entry  point.
  19.         Following  the "main" program name is a pair of  parentheses
  20.         which  are  an  indication to the compiler that  this  is  a
  21.         function.   We will cover exactly what a function is in  due
  22.         time.   For now,  I suggest that you simply include the pair
  23.         of parentheses.
  24.  
  25.              The  two curly brackets,  properly called  braces,  are
  26.         used to define the limits of the program itself.  The actual
  27.         program  statements  go between the two braces and  in  this
  28.         case,  there  are  no statements because  the  program  does
  29.         absolutely  nothing.  You can compile and run this  program,
  30.         but since it has no executable statements, it does  nothing.
  31.         Keep in mind however, that it is a valid C program.
  32.  
  33.                        A PROGRAM THAT DOES SOMETHING
  34.  
  35.              For  a much more interesting program,  load the program
  36.         named WRTSOME.C and display it on your monitor.   It is  the
  37.         same  as  the  previous  program  except  that  it  has  one
  38.         executable statement between the braces.
  39.  
  40.              The  executable  statement  is a  call  to  a  function
  41.         supplied  as a part of your C library.  Once again, we  will
  42.         not worry about what a function is, but only how to use this
  43.         one named "printf".  In order to output text to the monitor,
  44.         it  is  put within the function parentheses and  bounded  by
  45.         quotation  marks.   The  end  result  is  that  whatever  is
  46.         included  between the quotation marks will be  displayed  on
  47.         the monitor when the program is run.
  48.  
  49.              Notice the semi-colon at the end of the line.  C uses a
  50.         semi-colon as a statement terminator,  so the semi-colon  is
  51.         required  as  a  signal to the compiler that  this  line  is
  52.         complete.   This  program  is also executable,  so  you  can
  53.         compile  and  run  it to see if it does what  you  think  it
  54.         should.
  55.  
  56.  
  57.  
  58.                                  Page 6
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                       Chapter 2 - Getting started in C
  69.  
  70.  
  71.                       ANOTHER PROGRAM WITH MORE OUTPUT
  72.  
  73.              Load  the  program  WRTMORE.C and display  it  on  your
  74.         monitor  for an example of more output and another small but
  75.         important concept.  You will see that there are four program
  76.         statements  in  this program, each one being a call  to  the
  77.         function  "printf".   The top line will be  executed  first,
  78.         then the next, and so on, until the fourth line is complete.
  79.         The statements are executed in order from top to bottom.
  80.  
  81.              Notice  the funny character near the end of  the  first
  82.         line,  namely  the backslash.   The backslash is used in the
  83.         printf   statement  to  indicate  that  a  special   control
  84.         character  is  following.  In this case, the  "n"  indicates
  85.         that  a  "newline" is requested.  This is an  indication  to
  86.         return  the cursor to the left side of the monitor and  move
  87.         down  one  line.  It is commonly referred to as  a  carriage
  88.         return/line  feed.  Any place within text that  you  desire,
  89.         you  can put a newline character and start a new line.   You
  90.         could even put it in the middle of a word and split the word
  91.         between two lines.  The C compiler considers the combination
  92.         of the backslash and letter n as one character.
  93.  
  94.              A complete description of this program is now possible.
  95.         The  first  printf outputs a line of text  and  returns  the
  96.         carriage.   The  second printf outputs a line but  does  not
  97.         return  the carriage so that the third line is  appended  to
  98.         the second, then followed by two carriage returns, resulting
  99.         in a blank line.  Finally the fourth "printf" outputs a line
  100.         followed by a carriage return and the program is complete.
  101.  
  102.              Compile and run this program to see if it does what you
  103.         expect  it to do.   It would be a good idea at this time for
  104.         you to experiment by adding additional lines of printout  to
  105.         see if you understand how the statements really work.
  106.  
  107.                           LETS PRINT SOME NUMBERS
  108.  
  109.              Load  the  file named ONEINT.C and display  it  on  the
  110.         monitor for our first example of how to work with data in  a
  111.         C program.  The entry point "main" should be clear to you by
  112.         now as well as the beginning brace.  The first new thing  we
  113.         encounter is the line containing "int index;", which is used
  114.         to define an integer variable named "index".  The "int" is a
  115.         reserved  word  in  C, and can therefore  not  be  used  for
  116.         anything else.  It defines a variable that can have a  value
  117.         from -32768 to 32767 in most C compilers for microcomputers.
  118.         The variable name, "index", can be any name that follows the
  119.         rules for an identifier and is not one of the reserved words
  120.         for C.  The final character on the line, the semi-colon,  is
  121.         the statement terminator used in C.
  122.  
  123.  
  124.                                  Page 7
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                       Chapter 2 - Getting started in C
  135.  
  136.  
  137.  
  138.              Note  that, even though we have defined a variable,  we
  139.         have not yet assigned a value to it.  We will see in a later
  140.         chapter  that additional integers could also be  defined  on
  141.         the  same  line,  but we will  not  complicate  the  present
  142.         situation.
  143.  
  144.              Observing the main body of the program, you will notice
  145.         that  there are three statements that assign a value to  the
  146.         variable  "index",  but only one at a time.   The first  one
  147.         assigns the value of 13 to "index", and its value is printed
  148.         out.   (We will see how shortly.)  Later, the value of 27 is
  149.         assigned to "index",  and finally 10 is assigned to it, each
  150.         value  being  printed out.   It should be intuitively  clear
  151.         that  "index"  is  indeed  a variable  and  can  store  many
  152.         different  values.   Please note that many times  the  words
  153.         "printed  out" are used to mean "displayed on the  monitor".
  154.         You  will  find that in many cases  experienced  programmers
  155.         take  this  liberty,  probably due to the "printf"  function
  156.         being used for monitor display.
  157.  
  158.                           HOW DO WE PRINT NUMBERS
  159.  
  160.              To  keep  our promise,  let's return  to  the  "printf"
  161.         statements  for a definition of how they work.   Notice that
  162.         they are all identical and that they all begin just like the
  163.         "printf"  statements  we  have  seen  before.    The   first
  164.         difference occurs when we come to the % character.   This is
  165.         a  special character that signals the output routine to stop
  166.         copying characters to the output and do something different,
  167.         namely output a variable.   The % sign is used to signal the
  168.         output  of  many different types of variables, but  we  will
  169.         restrict  ourselves  to  only one  for  this  example.   The
  170.         character  following the % sign is a "d", which signals  the
  171.